package ca.uguu.unknown_space.worldobjects.background;

import ca.uguu.unknown_space.worldobjects.LaserWeapon;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;

public abstract class Weapon {
	final private String spritePath;
	final private WeaponSlot weaponSlot;
	
	public Weapon(WeaponDef weaponDef, WeaponSlot weaponSlot){
		//Loads the weapon's specs from disk
		
		this.weaponSlot = weaponSlot;
		this.spritePath = "data/weapons/" + weaponDef.name + '/' + this.getOrientationFilename();
	}
	private String getOrientationFilename(){
		if(this.weaponSlot.orientation == null){
			if(this.weaponSlot.height > this.weaponSlot.width){
				return "vertical.png";
			}else if(this.weaponSlot.width > this.weaponSlot.height){
				return "horizontal.png";
			}
			return "centre.png";
		}
		switch(this.weaponSlot.orientation){
			case UP: {
				return "up.png";
			}
			case DOWN: {
				return "down.png";
			}
			case LEFT: {
				return "left.png";
			}
			case RIGHT: {
				return "right.png";
			}
		}
		return "lawljava.png"; //Silly, untrusting Java
	}
	
	public boolean acquireTarget(){
		//Tries to find a target in the current world-space
		//Returns success
		//Call this before calling fire
		//(May be combined into the same function, but may also be left separate to optimise
		//power and damage algorithms)
		
		return false;
	}
	
	public boolean canFire(){
		//Some weapons build up power, which they can then use in successive discharges; some have cooldowns and use current resources
		//This might need to be implied by acquireTarget() and each weapon should be ticked by the ship. Dunno what the interaction will
		//look like yet.
		return false;
	}
	
	public float estimateDamage(){
		return this.estimateDamageShield() + this.estimateDamageHull() + this.estimateDamageSubsystem();
	}
	public abstract float estimateDamageShield();
	public abstract float estimateDamageHull();
	public abstract float estimateDamageSubsystem();
	
	public void draw(Pixmap surface){
		final Pixmap pixmap = new Pixmap(Gdx.files.internal(this.spritePath));
		//On not-found exception, do nothing; when loading weapons, treat them as null; ditto for ships
		surface.drawPixmap(pixmap, this.weaponSlot.getLeft(), this.weaponSlot.getTop());
		pixmap.dispose();
	}
	
	//Factory methods
	public static Weapon buildWeapon(WeaponDef weaponDef, WeaponSlot weaponSlot) throws Exception{
		if(weaponDef.type.equals("laser")){
			return new LaserWeapon(weaponDef, weaponSlot);
		}
		throw new IllegalArgumentException("Weapon-type not recognised: " + weaponDef.type);
	}
}
